package com.example.famhealing.lockscreen;

import java.io.InputStream;
import java.net.URL;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;

import com.example.famhealing.R;
import com.example.famhealing.R.id;
import com.example.famhealing.R.layout;
import com.example.famhealing.R.menu;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageButton;
import android.widget.ImageView;

public class LockScreenActivity extends Activity implements OnTouchListener {

	ImageButton ibutton;
	ImageView rightImg,leftImg;
	private final int START_DRAG = 0;
	private final int END_DRAG = 1;
	private int isMoving;
	String title="";
	String url="http://www.anticancerkorea.com";
	String scr_id="";
	String user_id="";
	String scr_pic_body="";
	String scr_pic_title="";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_lock_screen);
		getWindow().addFlags(
				WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
						| WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
						| WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

		StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().permitNetwork().build());
		
		try
		{
			URL url1=new URL("http://192.168.1.98:8080/famhealing/getArticle.get");
			InputStream in=url1.openStream();

			XmlPullParserFactory factory=XmlPullParserFactory.newInstance();
			XmlPullParser parser=factory.newPullParser();
			
			parser.setInput(in, "euc-kr");
			
			int eventType=parser.getEventType();
			boolean isItemTag=false;
			String tagName="";
			
			while(eventType!=XmlPullParser.END_DOCUMENT)
			{
				if(eventType==XmlPullParser.START_TAG)
				{
					tagName=parser.getName();
					if(!tagName.equals("data")&&!tagName.equals("scrap"))
						isItemTag=true;
				}
				else if(eventType==XmlPullParser.TEXT && isItemTag==true)
				{
					if(tagName.equals("scr_id"))
					{
						scr_id=parser.getText();
					}
					if(tagName.equals("user_id"))
					{
						user_id=parser.getText();
					}
					if(tagName.equals("title"))
					{
						title=parser.getText();
					}
					if(tagName.equals("url"))
					{
						url=parser.getText();
					}
					if(tagName.equals("scr_pic_title"))
					{
						scr_pic_title=parser.getText();
					}
					if(tagName.equals("scr_pic_body"))
					{
						scr_pic_body=parser.getText();
					}
				}
				else if(eventType==XmlPullParser.END_TAG)
					isItemTag=false;
				
				eventType=parser.next();
			}
		}
		catch(Exception e)
		{
			System.out.println(e);
		}
		ibutton = (ImageButton) findViewById(R.id.ex);
		ibutton.setOnTouchListener(this);
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.lock_screen, menu);
		return true;
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		
		System.out.println(url);
		int left = v.getLeft();
		int buttonSize=v.getRight()-v.getLeft();
		rightImg=(ImageView)findViewById(R.id.gm);
		leftImg=(ImageView)findViewById(R.id.gf);

		if (event.getAction() == MotionEvent.ACTION_DOWN) {
			isMoving = START_DRAG;
		} else if (event.getAction() == MotionEvent.ACTION_UP) {
			isMoving = END_DRAG;
			if(((int)event.getRawX()>=leftImg.getLeft()-30 && (int)event.getRawX()<=leftImg.getLeft()+30) || (int)event.getRawX()<=leftImg.getLeft())
			{
				Intent i = new Intent(Intent.ACTION_VIEW);
				Uri u = Uri.parse(url);
				i.setData(u);
				startActivity(i);
				finish();
			}
			else if(((int)event.getRawX()+buttonSize>=rightImg.getRight()-30 && (int)event.getRawX()+buttonSize<=rightImg.getRight()+30) || (int)event.getRawX()+buttonSize>=rightImg.getRight())
				finish();
			v.setX(left);
		} else if (event.getAction() == MotionEvent.ACTION_MOVE) {
			if (isMoving == START_DRAG) {
				v.setX((int)event.getRawX());
			}
		}
		return false;
	}
}
